from Stu import Student
class LinkList:
    def __init__(self, node=None):
        self.__head = None
        self.count = 0

    def is_empty(self):
        return self.__head == None

    def length(self):
        current = self.__head
        count = 0
        while current != None:
            count += 1
            current = current.next
        return count

    def travel(self):
        """遍历链表并返回所有学生列表"""
        students = []
        current = self.__head
        while current != None:
            students.append(current)
            current = current.next
        return students

    def add(self, xuehao, yuwen, shuxue, yingyu):
        """链表头部添加元素"""
        new_node = Student(xuehao, yuwen, shuxue, yingyu)
        new_node.next = self.__head
        self.__head = new_node
        self.count += 1

    def append(self, xuehao, yuwen, shuxue, yingyu):
        """链表尾部添加元素"""
        new_node = Student(xuehao, yuwen, shuxue, yingyu)
        if self.is_empty():
            self.__head = new_node
        else:
            current = self.__head
            while current.next != None:
                current = current.next
            current.next = new_node
        self.count += 1

    def insert(self, pos, xuehao, yuwen, shuxue, yingyu):
        """指定位置添加元素"""
        if pos > (self.length() - 1):
            self.append(xuehao, yuwen, shuxue, yingyu)
        elif pos <= 0:
            self.add(xuehao, yuwen, shuxue, yingyu)
        else:
            new_node = Student(xuehao, yuwen, shuxue, yingyu)
            pre = self.__head
            count = 0
            while count < (pos - 1):
                count += 1
                pre = pre.next
            new_node.next = pre.next
            pre.next = new_node
            self.count += 1

    def remove(self, xuehao):
        """根据学号删除节点"""
        current = self.__head
        pre = None
        while current != None:
            if current.xuehao == xuehao:
                if current == self.__head:
                    self.__head = current.next
                else:
                    pre.next = current.next
                self.count -= 1
                return True
            else:
                pre = current
                current = current.next
        return False

    def search(self, xuehao):
        """根据学号查找节点"""
        current = self.__head
        while current != None:
            if current.xuehao == xuehao:
                return current
            current = current.next
        return None

    def calculate_all_averages(self):
        """计算所有学生的平均分"""
        current = self.__head
        while current != None:
            current.pingjunshu = (current.yuwen + current.shuxue + current.yingyu) / 3
            current = current.next
    def zongchengji(self):
        """计算所有学生的总分"""
        zongchengji_list = []
        current = self.__head
        while current != None:
            current.zong = current.yuwen + current.shuxue + current.yingyu
            zongchengji_list.append(current.zong)
            current = current.next
        return zongchengji_list
    def maopaojiangxu(self):
        """按平均分降序排序返回学生列表"""
        students = self.travel()
        # 使用冒泡排序按平均分降序排列
        n = len(students)
        for i in range(n):
            for j in range(0, n - i - 1):
                if students[j].pingjunshu < students[j + 1].pingjunshu:
                    students[j], students[j + 1] = students[j + 1], students[j]
        return students
    def maopaoshengxu(self):
        """按平均分升序排序返回学生列表"""
        students = self.travel()
        # 使用冒泡排序按平均分升序排列
        n = len(students)
        for i in range(n):
            for j in range(0, n - i - 1):
                if students[j].pingjunshu > students[j + 1].pingjunshu:
                    students[j], students[j + 1] = students[j + 1], students[j]
        return students

    def xueshengliebiao(self):
        """按学号排序返回学生列表"""
        students = self.travel()
        # 使用冒泡排序按学号升序排列
        n = len(students)
        for i in range(n):
            for j in range(0, n - i - 1):
                if students[j].xuehao > students[j + 1].xuehao:
                    students[j], students[j + 1] = students[j + 1], students[j]
        return students